//
//  CollectionViewController.m
//  ZAKER
//
//  Created by dllo on 15/9/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "CollectionViewController.h"


@interface CollectionViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, retain) UITableView *tableView; // 创建tableView的属性
@property (nonatomic, retain) NSMutableArray *tableArr; // 创建可变数组接收解析的数据
@property (nonatomic, retain) UIAlertView *myAlert; // 如果没有收藏的内容, 提示.

@end

@implementation CollectionViewController

-(void)dealloc
{
    [_tableArr release];
    [_myAlert release];
    [_tableView release];
    
    [super dealloc];
    
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.tableArr = [NSMutableArray array];
        
    }
    
    return self;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"详情";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"t.png"] style:UIBarButtonItemStylePlain target:self action:@selector(left:)];
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self createTable];
}




- (void)viewDidAppear:(BOOL)animated
{
    self.tableArr = [NSMutableArray array];
    [super viewDidAppear:YES];
    _myAlert.hidden = YES;
    [self selectAllData];
    
}



#pragma mark - 搜索整个数据库
-(void)selectAllData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSArray *arr = [NovalDataBase selectAllHoliday];
            [self.tableArr addObjectsFromArray:arr];
            
            if ([arr count] == 0) {
                
                self.myAlert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"暂无收藏内容" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];

                [_myAlert show];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [_myAlert dismissWithClickedButtonIndex:0 animated:YES];
                    [self.navigationController popViewControllerAnimated:YES];
                    
                });
            }
            
            [self.tableView reloadData];
            
        });
    });
}


-(void)createTable
{
    self.tableView = [[[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _tableArr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *reuse = @"reuse";
    
    HolidayTableViewCell *hsCell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (hsCell == nil) {
        
        hsCell = [[[HolidayTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
        
    }
    
    DetailShowModel *model = [_tableArr objectAtIndex:indexPath.row ];
    
    hsCell.titleLabel.text = model.title;
    hsCell.addressLabel.text = model.address;
    [hsCell.MyImage sd_setImageWithURL:[NSURL URLWithString:model.imageUrlStr] placeholderImage:nil];
    hsCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return hsCell;
    
}

#pragma mark - 删除收藏

/* 1. 使TableVIew 处于编辑状态 */
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    
    /* 1. 父类执行 */
    [super setEditing:editing animated:animated];
    
    /* 2. 使 tableView 处于编辑状态 */
    [self.tableView setEditing:editing animated:animated];
    
}

/* 2. 指定tableView那些行可以编辑 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
    
}

/* 3. 指定编辑类型: 插入, 删除 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
    
}

/* 4. 确定编辑(先操作数据, 再操作UI(cell) ) */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        DetailShowModel *model = [_tableArr objectAtIndex:indexPath.row];
        [NovalDataBase deleteHolidayWithModel:model];
    
        [self.tableArr removeObjectAtIndex:indexPath.row];
           [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadData];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 240 * myHeight;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailShowModel *model = [_tableArr objectAtIndex:indexPath.row];
    EnyerViewController *enter = [[EnyerViewController alloc]init];
    [enter sendWithUrl:model.urlStr];
    [self.navigationController pushViewController:enter animated:YES];
    [enter release];
    
 }

- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
    
}



@end
